const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 系统菜单（tenant_id 为 null）
    const menus = [
      // ==== 一级菜单：系统管理 ====
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'system',
        title: '系统管理',
        english: 'System',
        type: 'directory',
        path: '/system',
        component: null,
        icon: 'SettingOutlined',
        permission: null,
        order_num: 100,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      // ==== 一级菜单：多租户管理 ====
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'tenant',
        title: '租户管理',
        english: 'Tenant',
        type: 'directory',
        path: '/tenant',
        component: null,
        icon: 'TeamOutlined',
        permission: null,
        order_num: 200,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    // 获取插入后的ID以便创建子菜单
    await queryInterface.bulkInsert('menus', menus);

    // 查询已插入的菜单ID
    const [systemMenu] = await queryInterface.sequelize.query(
      `SELECT id FROM menus WHERE name = 'system'`,
    );
    const [tenantMenu] = await queryInterface.sequelize.query(
      `SELECT id FROM menus WHERE name = 'tenant'`,
    );

    const systemId = systemMenu[0].id;
    const tenantId = tenantMenu[0].id;

    // 二级菜单和按钮
    const subMenus = [
      // === 系统管理子菜单 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'user',
        title: '用户管理',
        english: 'User Management',
        type: 'menu',
        path: '/system/user',
        component: 'views/system/user/index',
        icon: 'UserOutlined',
        permission: 'user:read',
        order_num: 1,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'role',
        title: '角色管理',
        english: 'Role Management',
        type: 'menu',
        path: '/system/role',
        component: 'views/system/role/index',
        icon: 'SafetyOutlined',
        permission: 'role:read',
        order_num: 2,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'permission',
        title: '权限管理',
        english: 'Permission Management',
        type: 'menu',
        path: '/system/permission',
        component: 'views/system/permission/index',
        icon: 'LockOutlined',
        permission: 'permission:read',
        order_num: 3,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'menu',
        title: '菜单管理',
        english: 'Menu Management',
        type: 'menu',
        path: '/system/menu',
        component: 'views/system/menu/index',
        icon: 'MenuOutlined',
        permission: 'menu:read',
        order_num: 4,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'audit-log',
        title: '审计日志',
        english: 'Audit Log',
        type: 'menu',
        path: '/system/audit-log',
        component: 'views/system/audit-log/index',
        icon: 'FileTextOutlined',
        permission: 'audit_log:read',
        order_num: 5,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: systemId,
        name: 'dictionary',
        title: '字典管理',
        english: 'Dictionary Management',
        type: 'menu',
        path: '/system/dictionary',
        component: 'views/system/dictionary/index',
        icon: 'BookOutlined',
        permission: 'dictionary:read',
        order_num: 6,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 租户管理子菜单 ===
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tenantId,
        name: 'tenant-list',
        title: '租户列表',
        english: 'Tenant List',
        type: 'menu',
        path: '/tenant/list',
        component: 'views/tenant/list/index',
        icon: 'UnorderedListOutlined',
        permission: 'tenant:read',
        order_num: 1,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: tenantId,
        name: 'plan',
        title: '套餐管理',
        english: 'Plan Management',
        type: 'menu',
        path: '/tenant/plan',
        component: 'views/tenant/plan/index',
        icon: 'CrownOutlined',
        permission: 'plan:read',
        order_num: 2,
        visible: true,
        status: true,
        cache: true,
        redirect: null,
        created_at: now,
        updated_at: now,
      },

      // === 按钮权限 ===
      // 用户管理按钮
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'user:create',
        title: '添加用户',
        english: 'Create User',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'user:create',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'user:update',
        title: '编辑用户',
        english: 'Edit User',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'user:update',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'user:delete',
        title: '删除用户',
        english: 'Delete User',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'user:delete',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      // 角色管理按钮
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'role:create',
        title: '添加角色',
        english: 'Create Role',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'role:create',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'role:update',
        title: '编辑角色',
        english: 'Edit Role',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'role:update',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'role:delete',
        title: '删除角色',
        english: 'Delete Role',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'role:delete',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        parent_id: null,
        name: 'role:assign-permission',
        title: '分配权限',
        english: 'Assign Permission',
        type: 'button',
        path: null,
        component: null,
        icon: null,
        permission: 'role:manage',
        order_num: 0,
        visible: true,
        status: true,
        cache: false,
        redirect: null,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('menus', subMenus);
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('menus', null, {});
  },
};

